package com.wsoban.jpush;

import android.content.Context;
import android.text.TextUtils;

import java.util.LinkedHashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;


/**
 * Description : JPush工具
 *
 * @author WSoban
 * @date 2019/8/23
 */
public class JPushSetUtil {

    private static JPushSetUtil sInstance;
    private int sequence = 1;
    private Context mContext;

    public static synchronized JPushSetUtil getInstance() {
        if (sInstance == null) {
            sInstance = new JPushSetUtil();
        }
        return sInstance;
    }

    /**
     * 在项目Application中做初始化
     *
     * @param context
     */
    public void init(Context context) {
        this.mContext = context;
        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(context);            // 初始化 JPush
    }

    /**
     * 获取RegistrationID
     *
     * @return
     */
    public String getRegistrationID() {
        return JPushInterface.getRegistrationID(mContext);

    }

    /**
     * 设置设备别名
     *
     * @param alias
     */
    public void setAlias(String alias) {
        onTagAliasAction(alias, true, TagAliasOperatorHelper.ACTION_SET);
    }

    /**
     * 设置设备标签
     *
     * @param tags
     */
    public void setTags(String tags) {
        onTagAliasAction(tags, false, TagAliasOperatorHelper.ACTION_SET);
    }

    /**
     * 删除别名
     *
     * @param alias
     */
    public void deleteAlias(String alias) {
        onTagAliasAction(alias, true, TagAliasOperatorHelper.ACTION_DELETE);
    }

    /**
     * 删除标签
     *
     * @param tags
     */
    public void deleteTags(String tags) {
        onTagAliasAction(tags, false, TagAliasOperatorHelper.ACTION_DELETE);
    }

    /**
     * 执行设置事件
     *
     * @param aliasOrTags
     * @param isAliasAction
     * @param action
     */
    private void onTagAliasAction(String aliasOrTags, boolean isAliasAction, int action) {
        TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
        if (isAliasAction) {
            tagAliasBean.alias = getAlias(aliasOrTags);
        } else {
            tagAliasBean.tags = getTags(aliasOrTags);
        }
        tagAliasBean.isAliasAction = isAliasAction;
        tagAliasBean.action = action;
        sequence++;
        TagAliasOperatorHelper.getInstance().handleAction(mContext, sequence, tagAliasBean);
    }

    /**
     * 检查别名格式
     *
     * @param alias
     * @return
     */
    private String getAlias(String alias) {
        if (TextUtils.isEmpty(alias)) {
            return null;
        }
        if (!ExampleUtil.isValidTagAndAlias(alias)) {
            return null;
        }
        return alias;
    }

    /**
     * 检查标签格式
     *
     * @param tags
     * @return
     */
    private Set<String> getTags(String tags) {
        if (TextUtils.isEmpty(tags)) {
            return null;
        }
        // "|"隔开的多个转换成 Set
        String[] sArray = tags.split("\\|");
        Set<String> tagSet = new LinkedHashSet<String>();
        for (String sTagItme : sArray) {
            if (!ExampleUtil.isValidTagAndAlias(sTagItme)) {
                return null;
            }
            tagSet.add(sTagItme);
        }
        if (tagSet.isEmpty()) {
            return null;
        }
        return tagSet;
    }

    /**
     * 设置推送服务开关
     *
     * @param isOpenPush 是否开启推送
     */
    public void setPushMessage(boolean isOpenPush) {
        if (isOpenPush) {
            //恢复推送服务
            JPushInterface.resumePush(mContext);
        } else {
            //停止推送服务
            JPushInterface.stopPush(mContext);
        }
    }

    /**
     * 用来检查推送服务是否已经被停止
     */
    public boolean isPushStopped() {
        return JPushInterface.isPushStopped(mContext);
    }
}
